package cabbage.database.utilities;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DataBaseConnection {

	public DataBaseConnection() {

	}
	
	public Connection getConnection() {
		//System.out.println("-------- MySQL JDBC Connection Testing ------------");

//		Connection connection = null;
//		String address = "jdbc:mysql://localhost:";
//		String portNumber = "3306";
//		String databaseName = "/cabbagedb";
//		String username = "root";
//		String password = "";
//		String connectionString = address + portNumber + databaseName;
		
		
		Context ctx;
		Connection connection = null;
		try {
			ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/cabbagedb");
			connection = ds.getConnection();
		} catch (NamingException e) {
			throw new RuntimeException(e);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
//		Connection connection = null;
//		String address = "jdbc:mysql://localhost:";
//		String portNumber = "8889";
//		String databaseName = "/cabbagedb";
//		String username = "root";
//		String password = "root";
//		String connectionString = address + portNumber + databaseName;
//
//		try {
//			Class.forName("com.mysql.jdbc.Driver");
//		} catch (ClassNotFoundException e) {
//			System.out.println("Where is your MySQL JDBC Driver?");
//			e.printStackTrace();
//			return connection;
//		}
//
//		//System.out.println("MySQL JDBC Driver Registered!");
//
//
//		try {
//			connection = DriverManager
//					.getConnection(connectionString,username,password);
//
//		} catch (SQLException e) {
//			System.out.println("Connection Failed! Check output console");
//			e.printStackTrace();
//			return connection;
//		}
//
//		if (connection != null) {
//			//System.out.println("You made it, take control your database now!");
//		} else {
//			System.out.println("Failed to make connection!");
//		}

		return connection;

	}

//	public Connection getConnection() {
//		//System.out.println("-------- MySQL JDBC Connection Testing ------------");
//
////		Connection connection = null;
////		String address = "jdbc:mysql://localhost:";
////		String portNumber = "3306";
////		String databaseName = "/cabbagedb";
////		String username = "root";
////		String password = "";
////		String connectionString = address + portNumber + databaseName;
////		
//		Connection connection = null;
//		String address = "jdbc:mysql://localhost:";
//		String portNumber = "8889";
//		String databaseName = "/cabbagedb";
//		String username = "root";
//		String password = "root";
//		String connectionString = address + portNumber + databaseName;
//
//		try {
//			Class.forName("com.mysql.jdbc.Driver");
//		} catch (ClassNotFoundException e) {
//			System.out.println("Where is your MySQL JDBC Driver?");
//			e.printStackTrace();
//			return connection;
//		}
//
//		//System.out.println("MySQL JDBC Driver Registered!");
//
//
//		try {
//			connection = DriverManager
//					.getConnection(connectionString,username,password);
//
//		} catch (SQLException e) {
//			System.out.println("Connection Failed! Check output console");
//			e.printStackTrace();
//			return connection;
//		}
//
//		if (connection != null) {
//			//System.out.println("You made it, take control your database now!");
//		} else {
//			System.out.println("Failed to make connection!");
//		}
//
//		return connection;
//
//	}

}
